package Introspector;

import reflection.User;

import java.beans.*;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @Author: dch
 * @Date: 2020/5/18 11:00
 * @Description:
 */
public class IntrospectorDemo {
    public static void main(String[] args) throws IntrospectionException {

        //获取userBeanInfo信息
        BeanInfo userBeanInfo = Introspector.getBeanInfo(User.class);
        System.out.println("【userBeanInfo】"+userBeanInfo);

        //获取属性描述
        PropertyDescriptor[] propertyDescriptors = userBeanInfo.getPropertyDescriptors();
        System.out.println("============属性描述===========");
        //利用Stream流进行循环输出
        Stream.of(propertyDescriptors).forEach(System.out::println);

        //获取方法描述
        MethodDescriptor[] methodDescriptors = userBeanInfo.getMethodDescriptors();
        System.out.println("============方法描述===========");
        //利用Stream流进行循环输出
        Stream.of(methodDescriptors).forEach(System.out::println);

        //获取事件描述
        EventSetDescriptor[] eventSetDescriptors = userBeanInfo.getEventSetDescriptors();
        System.out.println("============事件描述===========");
        //利用Stream流进行循环输出
        Stream.of(eventSetDescriptors).forEach(System.out::println);

        //其他API的使用
        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("name","tudedong");
        User user = new User(3,"晓东");
        //测试
        map2Object(map,user);
        System.out.println("=======将map转化为object========");
        System.out.println(user);
    }

    /**
     *  工具类：将map转化为object
     * @param map
     * @param object
     */
    public static void map2Object(Map<String,Object> map,Object object){
        try {
            //通过内省获得object的Beaninfo信息
            BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
            //获取属性描述
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            //获取方法描述
            MethodDescriptor[] methodDescriptors = beanInfo.getMethodDescriptors();
            //遍历map，获取所有需要封装的参数
            Iterator<Map.Entry<String,Object>> iterator = map.entrySet().iterator();
            String key = null;
            Object value = null;
            while(iterator.hasNext()){
                Map.Entry<String, Object> next = iterator.next();
                key = next.getKey();
                value = next.getValue();
                for(PropertyDescriptor pro:propertyDescriptors){
                    if(!pro.getName().equals(key)){
                        continue;
                    }
                    //调用javabean的写方法对字段进行赋值
                    pro.getWriteMethod().invoke(object,value);
                }
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
